<?php
class CatAction extends BaseAction{
	  
	  private $catModel;
    function _initialize() {
    	parent::_initialize();
    	$this->catModel = D('Cat');
    }
    //栏目列表
    public function catList(){
    	$cdata = $this->catModel->getAllCats();
    	$this->assign("list",$cdata);
    	$this->display();
    }
    
    //新增栏目页面
    public function addCat(){
    	
    	if(!empty($_GET['pid'])){
    		$cat = $this->catModel->getCatById($_GET['pid']);
    		$this->assign("cat",$cat);
    	}
    	$this->assign("models",D('Model')->getModel());
    	$this->display();
    }
    
    
    //封装栏目下拉框
    public function getCats(){
    	
    	echo '<select name="cat" id="cat">'.
					  '<option value="0">请选择分类</option>';

    	$cdata = $this->catModel->getTypeCats($_GET['type']);
    	
    	foreach($cdata as $cd){
    		if($cd['type']!='Spage'){
	    			 echo '<option value="'.$cd['id'].'">';
	    			 for($i=0;$i<$cd['depth'];$i++) echo '--';
	    			 echo ' '.$cd['cname'].'</option>';
    		}
 
    	}
    	echo '</select>';
    }
    
    
    
    //添加分类
    public function doAddCat(){
    	
				//$cat = D('Cat');
				$cat = $this->catModel->where("directory='".$_POST['directory']."'")->find();
				if($cat['id']>0){
					$this->error('目录已经存在，请重新填写！');
					return;
				}
				if($this->catModel->create()){
					$model = D('Model')->getModelByTable($_POST['type']);
					$info = upload('cat',$model['cwidth'],$model['cheight']); //上传栏目图片
					if($info){
						$this->catModel->img = 'cat/thumb_'.$info[0]['savename'];
				  }
					
					//为空添加顶级分类
					if(empty($_POST['pid'])){
						$this->catModel->pid = 0;
						$this->catModel->depth = 0;
						
					}else{
						$tempcat = $this->catModel->getCatById($_POST['pid']);
						$this->catModel->depth  = $tempcat['depth']+1;
					}
					if($this->catModel->add()){
						  S('catDatas',NULL); //分类缓存清空
							$this->assign("jumpUrl","__URL__/catList");
					    $this->success("添加栏目成功！");
						}else{
							$this->error('添加失败！');
						}
				}else{
					$this->error($this->catModel->getError());
				}
    }
    
    public function editCat(){
    	$ncat = $this->catModel->where("id<>".$_POST['id']." and directory='".$_POST['directory']."'")->find();
			if($ncat['id']>0){
					$this->error('目录已经存在，请重新填写！');
					return;
			}
    	$cat = $this->catModel->getCatById($_GET['pid']);
    	if($cat){
    		if($cat['pid']>0){
    			$pcat = $this->catModel->getCatById($cat['pid']);
    			$this->assign('pcat',$pcat);
    		}
    		$this->assign("model",D('Model')->getModelByTable($cat['type']));
    		$this->assign("models",D('Model')->getModel());
    		$this->assign('cat',$cat);
    		$this->display();
    	}else{
    		$this->error('栏目不存在！！');
    	}
    }
    
    public function doEditCat(){
    			if($this->catModel->create()){
					$model = D('Model')->getModelByTable($_POST['type']);
					$info = upload('cat',$model['cwidth'],$model['cheight']); //上传栏目图片
					if($info){
						$this->catModel->img = 'cat/thumb_'.$info[0]['savename'];
				  }
					
					//为空添加顶级分类
					/**
					if(empty($_POST['pid'])){
						$this->catModel->pid = 0;
						$this->catModel->depth = 0;
						
					}else{
						$tempcat = $this->catModel->getCatById($_POST['pid']);
						$this->catModel->depth  = $tempcat['depth']+1;
					}
					**/
					if($this->catModel->save()){
						  S('catDatas',NULL); //分类缓存清空
							$this->assign("jumpUrl","__URL__/catList");
					    $this->success("修改栏目成功！");
						}else{
							$this->error('修改失败！');
						}
				}else{
					$this->error($this->catModel->getError());
				}
    }
    
    public function delCat(){
    	
    	$cats = $this->catModel->where('id='.$_GET['id'])->delete();
    	if($cats){
    		 $this->catModel->where('pid='.$_GET['id'])->delete();
    		 S('catDatas',NULL); //分类缓存清空
    		 $this->success('栏目删除成功！！');
    	}else{
    		$this->error('栏目删除失败！！');
    	}
    	
    }
   
}
?>